import 'package:connectivity/connectivity.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:video_player/video_player.dart';
import 'package:marquee_flutter/marquee_flutter.dart';
import 'package:wanchong/conf.dart';
import 'package:wanchong/public.dart';
import 'package:wanchong/utils/common.dart';
import 'package:wanchong/widget/ConnectWidget.dart';

class Videos extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _VideoState();
  }
}

class _VideoState extends State<Videos> {
  bool playshow = false;
  var playList = [
    'http://zhmc.yyang.xin/4782700501b4aa98a4b2c4dd57b8056b.mp4',
    'http://zhmc.yyang.xin/4782700501b4aa98a4b2c4dd57b8056b.mp4',
    'http://zhmc.yyang.xin/4782700501b4aa98a4b2c4dd57b8056b.mp4',
    'http://zhmc.yyang.xin/4782700501b4aa98a4b2c4dd57b8056b.mp4',
    'http://zhmc.yyang.xin/4782700501b4aa98a4b2c4dd57b8056b.mp4',
  ];
  bool _isInit = false;
  var a = ['a1','a2'];
  VideoPlayerController controllers;
  _VideoState() {
    controllers = VideoPlayerController.network(
      'http://zhmc.yyang.xin/4782700501b4aa98a4b2c4dd57b8056b.mp4',
    );
    controllers.setLooping(true);
    controllers.setVolume(1.0);
  }

  @override
  void initState() {
    super.initState();
    int startWife = 0;
    int startMobild = 0;
    eventBus.on(NetState, (value) {
      if (value == ConnectivityResult.wifi) {
        //alert("当前网络wifi");
        startMobild = 0;
        if (startWife == 0) {
          this.playshow = false;
          startWife = 1;
          controllers.play();
        }
      } else if (value == ConnectivityResult.mobile) {
        startWife = 0;
        if (startMobild == 0) {
          startMobild = 1;
          this.playshow = true;
          controllers.pause();
          alert('当前移动网络,点击播放');
        }
      } else if (value == ConnectivityResult.none) {
        //alert("当前网络不可用");
      } else {
        //alert("当前网络不可用");
      }
    });
    controllers.initialize().then((value) {
      setState(() {
        _isInit = controllers.value.initialized;
      });
    });
  }

  @override
  void dispose() {
    controllers.dispose();
    eventBus.off(NetState);
    super.dispose();
  }

  ///创建播放中的视频界面
  Widget _buildPlayingWidget() {
    return AspectRatio(
        aspectRatio: controllers.value.aspectRatio, //自适应宽度
        child: GestureDetector(
          onTap: () {
            if (controllers.value.isPlaying) {
              controllers.pause();
              setState(() {
                this.playshow = true;
              });
            } else {
              controllers.play();
              setState(() {
                this.playshow = false;
              });
            }
          },
          child: VideoPlayer(controllers),
        ));
  }

  ///视频正在加载的界面
  Widget _buildInitingWidget() {
    return AspectRatio(
      aspectRatio: 3 / 2,
      child: Stack(
        children: <Widget>[
          VideoPlayer(controllers),
          const Center(child: CircularProgressIndicator()),
        ],
        fit: StackFit.expand,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new PreferredSize(
        child: new Container(
          color: Colors.black,
          height: 10.0,
        ),
        preferredSize: new Size(MediaQuery.of(context).size.width, 150.0),
      ),
      body: Container(
        color: Color(0XFF000000),
        child: Stack(
          children: <Widget>[
            Center(
              child: _isInit ? _buildPlayingWidget() : _buildInitingWidget(),
            ),
            Positioned(
              right: ScreenUtil.getInstance().setSp(15.0),
              top: ScreenUtil.getInstance().setSp(40.0),
              child: Container(
                width: ScreenUtil.getInstance().setSp(30.0),
                height: ScreenUtil.getInstance().setSp(30.0),
                child: GestureDetector(
                  onTap: () {
                    Navigator.pop(context);
                  },
                  child: CircleAvatar(
                    backgroundColor: Color(0x00000000),
                    backgroundImage: new AssetImage(
                        'assets/livevideo/equipment_saomiao_top_close_icon.png'),
                    radius: ScreenUtil.getInstance().setSp(30.0),
                  ),
                ),
              ),
            ),
            Positioned(
              left: ScreenUtil.getInstance().setSp(15.0),
              top: ScreenUtil.getInstance().setSp(30.0),
              child: Container(
                child: Row(
                  children: <Widget>[
                    Container(
                      width: ScreenUtil.getInstance().setSp(50.0),
                      height: ScreenUtil.getInstance().setSp(50.0),
                      child: CircleAvatar(
                        backgroundImage: new NetworkImage(
                            'http://pic37.nipic.com/20140113/8800276_184927469000_2.png'),
                        radius: ScreenUtil.getInstance().setSp(50.0),
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(
                          left: ScreenUtil.getInstance().setSp(5.0)),
                      child: Text(
                        '我是谁我在那我是谁我在那我是谁我在那',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: ScreenUtil.getInstance().setSp(18.0),
                          fontWeight: FontWeight.w700,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
            Positioned(
              bottom: ScreenUtil.getInstance().setSp(150.0),
              left: ScreenUtil.getInstance().setSp(15.0),
              child: Container(
                height: ScreenUtil.getInstance().setSp(30.0),
                width: ScreenUtil.getInstance().setSp(125.0),
                alignment: Alignment.center,
                margin: EdgeInsets.only(left: 0.0),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(
                      ScreenUtil.getInstance().setSp(30.0)),
                  color: Color(0xFFFFCC33),
                ),
                child: new MarqueeWidget(
                  text: "我是谁我在那我是谁我在那我是谁我在那",
                  textStyle: new TextStyle(
                      fontSize: ScreenUtil.getInstance().setSp(15.0),
                      color: Color(0xFF111111)),
                  scrollAxis: Axis.horizontal,
                ),
              ),
            ),
            Positioned(
              bottom: ScreenUtil.getInstance().setSp(80.0),
              left: ScreenUtil.getInstance().setSp(15.0),
              child: Container(
                height: ScreenUtil.getInstance().setSp(50.0),
                width: ScreenUtil.getInstance().setSp(250.0),
                child: Text(
                  '我是谁我在那我是谁我在那我是谁我在那我是谁我在那我是谁我在那我是谁我在那',
                  textAlign: TextAlign.left,
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                    fontSize: ScreenUtil.getInstance().setSp(15.0),
                    color: Colors.white,
                  ),
                ),
              ),
            ),
            Positioned(
              bottom: ScreenUtil.getInstance().setSp(100.0),
              right: ScreenUtil.getInstance().setSp(15.0),
              child: Container(
                width: ScreenUtil.getInstance().setSp(30.0),
                height: ScreenUtil.getInstance().setSp(30.0),
                child: Image.asset(
                  'assets/livevideo/home_icon_dianzan_a.png',
                  width: ScreenUtil.getInstance().setSp(30.0),
                  height: ScreenUtil.getInstance().setSp(30.0),
                  fit: BoxFit.cover,
                ),
              ),
            ),
            Positioned(
              bottom: ScreenUtil.getInstance().setSp(30.0),
              right: ScreenUtil.getInstance().setSp(15.0),
              child: Container(
                width: ScreenUtil.getInstance().setSp(30.0),
                height: ScreenUtil.getInstance().setSp(30.0),
                child: Image.asset(
                  'assets/livevideo/home_icon_gengduo.png',
                  width: ScreenUtil.getInstance().setSp(30.0),
                  height: ScreenUtil.getInstance().setSp(30.0),
                  fit: BoxFit.cover,
                ),
              ),
            ),
            Positioned(
              bottom: ScreenUtil.getInstance().setSp(30.0),
              left: ScreenUtil.getInstance().setSp(15.0),
              child: Container(
                alignment: Alignment.centerLeft,
                width: ScreenUtil.getInstance().setSp(300.0),
                height: ScreenUtil.getInstance().setSp(30.0),
                child: Text(
                  '1020  阅读量      300 个赞',
                  style: TextStyle(
                    color: Color(0xFFB4BEC6),
                    fontSize: ScreenUtil.getInstance().setSp(12.0),
                  ),
                ),
              ),
            ),
            Positioned(
              child: ConnectWidget(),
            ),
            GestureDetector(
              onTap: () {
                if (controllers.value.isPlaying) {
                  controllers.pause();
                  setState(() {
                    this.playshow = true;
                  });
                } else {
                  controllers.play();
                  setState(() {
                    this.playshow = false;
                  });
                }
              },
              child: Align(
                alignment: Alignment.center,
                child: Image.asset(
                  'assets/ic_playing.png',
                  height: playshow
                      ? ScreenUtil.getInstance().setSp(60.0)
                      : ScreenUtil.getInstance().setSp(0.0),
                  width: ScreenUtil.getInstance().setSp(60.0),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
